package com.litidea.actions;

import static com.litidea.commons.keys.SystemKeys.DEFAULT_JSONARRAY_STR;
import static com.litidea.commons.keys.SystemKeys.PUB_ADD_OPTION;
import static com.litidea.commons.keys.SystemKeys.PUB_DEL_OPTION;
import static com.litidea.commons.keys.SystemKeys.PUB_EDIT_OPTION;
import static com.litidea.commons.keys.SystemKeys.PUB_SELECTED;
import static com.litidea.commons.keys.SystemKeys.PUB_WAIT_SELECT;
import static com.litidea.commons.paramkeys.BasicDataParamKeys.SAVE_CHANGE_TYPE_PARAMS;
import static com.litidea.commons.paramkeys.BasicDataParamKeys.SAVE_CHANGE_TYPE_TEST_ITEMS_PARAMS;
import static com.litidea.commons.paramkeys.BasicDataParamKeys.SAVE_TEST_ITEMS_PARAMS;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.litidea.dao.BasicDataDAO;
import com.litidea.dataobjects.ChangeTypeDO;
import com.litidea.dataobjects.ChangeTypeTestItemsDO;
import com.litidea.dataobjects.DepartmentDO;
import com.litidea.dataobjects.KValueDO;
import com.litidea.dataobjects.TestItemDO;
import com.litidea.dataobjects.UserLoginDO;
import com.opensymphony.xwork2.Action;

public class BasicDataManageAction extends BaseAction implements Action {

	/**
	 * 序列码
	 */
	private static final long serialVersionUID = 968674751758541095L;

	private BasicDataDAO basicDataDAO;
	
	/**
	 * 获取测试项目列表
	 * 
	 * @return
	 */
	public String getTestItemsKV() {
		Map<String, String> params = null;
		try {
			List<TestItemDO> list = basicDataDAO.getTestItems(params);
			
			List<KValueDO> realList = convertTestItemsToKV(list);
			return responseAjax(realList);
		} catch (Exception e) {
			errorInfo = e.getMessage();
		}
		return null;
	}
	
	/**
	 * 将部门信息转换为KV结构
	 * 
	 * @param list
	 * @return
	 */
	private List<KValueDO> convertTestItemsToKV(List<TestItemDO> list) {
		List<KValueDO> realList = new ArrayList<KValueDO>(list.size());
		for (TestItemDO testItemDO : list) {
			realList.add(new KValueDO(testItemDO.getId().toString(), testItemDO.getName()));
		}
		return realList;
	}
	
	/**
	 * 获取变更类别列表
	 * 
	 * @return
	 */
	public String getChangeTypesKV() {
		Map<String, String> params = new HashMap<String, String>();
		try {
			// 填充用户产业线信息
			// 获取登录用户的产业线
			UserLoginDO userLoginDO = this.findUserInfo();
			DepartmentDO departmentDO = this.getProductLineDept(String.valueOf(userLoginDO.getDeptId()));
			if(departmentDO == null) {
				params.put("productLine", "-1");
			}else{
				params.put("productLine", String.valueOf(departmentDO.getId()));
			}
			
			// 只显示已发布的
			params.put("isPublished", "1");
			List<ChangeTypeDO> list = basicDataDAO.getChangeTypes(params);
			
			List<KValueDO> realList = convertChangeTypesToKV(list);
			return responseAjax(realList);
		} catch (Exception e) {
			errorInfo = e.getMessage();
		}
		return null;
	}
	
	/**
	 * 将变更类别信息转换为KV结构
	 * 
	 * @param list
	 * @return
	 */
	private List<KValueDO> convertChangeTypesToKV(List<ChangeTypeDO> list) {
		List<KValueDO> realList = new ArrayList<KValueDO>(list.size());
		for (ChangeTypeDO changeTypeDO : list) {
			realList.add(new KValueDO(changeTypeDO.getId().toString(), changeTypeDO.getName()));
		}
		return realList;
	}

	/**
	 * 获取测试项目列表
	 * 
	 * @return
	 */
	public String getTestItems() {
		Map<String, String> params = getParameters(SAVE_TEST_ITEMS_PARAMS);
		try {
			fillPageParams(params);
			return responseAjax(basicDataDAO.getTestItemsPageList(params)
					.toString());
		} catch (Exception e) {
			errorInfo = e.getMessage();
		}
		return null;
	}

	/**
	 * 保存测试项目信息
	 * 
	 * @return
	 */
	public String saveTestItem() {
		Map<String, String> params = getParameters(SAVE_TEST_ITEMS_PARAMS);
		String success = null, errorInfo = null;
		try {
			int type = Integer.valueOf(params.get("type"));
			switch (type) {
			case PUB_ADD_OPTION:
				success = basicDataDAO.addNewTestItem(params);
				break;
			case PUB_EDIT_OPTION:
				success = basicDataDAO.editTestItem(params);
				break;
			case PUB_DEL_OPTION:
				Integer id = Integer.valueOf(params.get("id"));
				success = basicDataDAO.delTestItem(id);
				break;
			}
		} catch (Exception e) {
			errorInfo = e.getMessage();
		}
		return normalResponse(errorInfo, success);
	}
	
	/**
	 * 获取变更类型列表
	 * 
	 * @return
	 */
	public String getChangeTypes() {
		Map<String, String> params = getParameters(SAVE_CHANGE_TYPE_PARAMS);
		try {
			fillPageParams(params);
			// 填充产业线信息
			// 获取登录用户的产业线
			UserLoginDO userLoginDO = this.findUserInfo();
			DepartmentDO departmentDO = this.getProductLineDept(String.valueOf(userLoginDO.getDeptId()));
			if(departmentDO == null) {
				params.put("productLine", "-1");
			}else{
				params.put("productLine", String.valueOf(departmentDO.getId()));
			}
			return responseAjax(basicDataDAO.getChangeTypesPageList(params)
					.toString());
		} catch (Exception e) {
			errorInfo = e.getMessage();
		}
		return null;
	}

	/**
	 * 保存变更类型信息
	 * 
	 * @return
	 */
	public String saveChangeType() {
		Map<String, String> params = getParameters(SAVE_CHANGE_TYPE_PARAMS);
		String success = null, errorInfo = null;
		
		// 获取登录用户的产业线
		UserLoginDO userLoginDO = this.findUserInfo();
		DepartmentDO departmentDO = this.getProductLineDept(String.valueOf(userLoginDO.getDeptId()));
		if(departmentDO == null) {
			errorInfo = "用户不属于产业线！";
			return normalResponse(errorInfo, success);
		}
		
		try {
			int type = Integer.valueOf(params.get("type"));
			params.put("productLine", String.valueOf(departmentDO.getId()));
			switch (type) {
			case PUB_ADD_OPTION:
				params.put("createdBy", String.valueOf(userLoginDO.getId()));
				success = basicDataDAO.addNewChangeType(params);
				break;
			case PUB_EDIT_OPTION:
				params.put("lastModifiedBy", String.valueOf(userLoginDO.getId()));
				success = basicDataDAO.editChangeType(params);
				break;
			case PUB_DEL_OPTION:
				Integer id = Integer.valueOf(params.get("id"));
				success = basicDataDAO.delChangeType(id);
				break;
			}
		} catch (Exception e) {
			errorInfo = e.getMessage();
		}
		return normalResponse(errorInfo, success);
	}

	/**
	 * 根据变更类别获取实验项目
	 * 
	 * @return
	 */
	public String getTestItemsByChangeType() {
		int type = getInt("type");
		List<TestItemDO> list = null;
		try {
			switch (type) {
			case PUB_WAIT_SELECT:
				list = basicDataDAO.getNotChangeTypeTestItems(getInt("id"));
				break;
			case PUB_SELECTED:
				list = basicDataDAO.getChangeTypeTestItemss(getInt("id"));
				break;
			}
			;
			if (list == null) {
				return responseAjax(DEFAULT_JSONARRAY_STR);
			}
			return responseAjax(list);
		} catch (Exception e) {
			return responseAjax(DEFAULT_JSONARRAY_STR);
		}
	}
	
	/**
	 * 保存变更类型的实验项目信息
	 * 
	 * @return
	 */
	public String saveChangeTypeTestItems() {
		String success = "设置实验项目操作成功。", errorInfo = null;
		try {
			basicDataDAO.saveChangeTypeTestItems(getInt("changeTypeId"), getStrings("testItems"));
		} catch (Exception e) {
			errorInfo = e.toString();
		}
		return normalResponse(errorInfo, success);
	}
	
	/**
	 * 查看变更类型的实验项目
	 * @return
	 */
	public String viewChangeTypeTestItems(){
		Integer changeId = getInt("changeTypeId");
		List<ChangeTypeTestItemsDO> list = basicDataDAO.getTestItemssByChangeId(changeId);
		return responseAjax(list);
	}
	
	/**
	 * 更新可选必选
	 * @return
	 */
	public String updateIsMust(){
		Map<String, String> params = this.getParameters(SAVE_CHANGE_TYPE_TEST_ITEMS_PARAMS);
		String success = "操作成功。", errorInfo = null;
		try {
			basicDataDAO.updateIsMust(params);
		} catch (Exception e) {
			errorInfo = e.toString();
		}
		return normalResponse(errorInfo, success);		
	}
	
	/**
	 * 发布变更类型
	 * @return
	 */
	public String publish(){
		String success = "发布操作成功。", errorInfo = null;
		Map<String, String> params = this.getParameters(SAVE_CHANGE_TYPE_PARAMS);
		try {
			basicDataDAO.isPublish(params);
		} catch (Exception e) {
			errorInfo = e.toString();
		}
		return normalResponse(errorInfo, success);	
	}

	public BasicDataDAO getBasicDataDAO() {
		return basicDataDAO;
	}

	public void setBasicDataDAO(BasicDataDAO basicDataDAO) {
		this.basicDataDAO = basicDataDAO;
	}
}
